<?php
class api extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->upload_groupObj = D('upload_group');
		$this->upload_fileObj = D('upload_file');

		 // 存储引擎
		 $this->config = $this->setting['storage'];
		 $this->storage = $this->config['default'];
	}

	public function base64_image_content($base64_image_content){
		$path = './upload/';
		//匹配出图片的格式
		if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
			//后缀 
			$type = $result[2];
			//创建文件夹，以年月日
			$new_file = $path.date('Ymd',time())."/";
			if(!file_exists($new_file)){
				//检查是否有该文件夹，如果没有就创建，并给予最高权限
				mkdir($new_file, 0700);
			}
			$new_file = $new_file.time().".{$type}";	//图片名以时间命名
			//保存为文件
			if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
				//返回这个图片的路径
				return $new_file;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	public function base64_to_filedata($base64_image_content){
		//匹配出图片的格式
		if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
			$filedata = base64_decode(str_replace($result[1], '', $base64_image_content));
			return $filedata;
		}else{
			return false;
		}
	}

	public function upload()
    {	
		$group_id = arg('group_id');

		$fileInfo['name'] = arg('name');
		$fileInfo['type'] = arg('type');
		$fileInfo['size'] = arg('size');
		$fileInfo['base64'] = arg('base64');
        
        if($fileInfo['base64']){
			// $filedata = $this->base64_to_filedata($fileInfo['base64']);
			// dump($filedata);exit;
			$fileInfo['tmp_name'] = $this->base64_image_content($fileInfo['base64']);
		}

		$result = $this->upload_fileObj->upload_image($this->config,$fileInfo);
		if($result){
			if(is_file($fileInfo['tmp_name'])){
				@unlink($fileInfo['tmp_name']);
			}

			$fileDir = $result['fileDir'];
			$fileName = $result['fileName'];
			// 添加文件库记录
			$uploadFile = $this->addUploadFile($group_id, $fileDir, $fileName, $fileInfo, 'image');
			$this->renderSuccess('图片上传成功', '', $uploadFile['file_id']);
			exit;
		}else{
			$this->renderError('图片上传失败');
		}

	}
	
	public function remove() {
		$fileIds[] = arg('file_id');
        $res = $this->upload_fileObj->softDelete($fileIds,0);
        if ($res) {
			foreach($fileIds as $val){
				$file = $this->upload_fileObj->get_image_url($val);
				if(is_file('.'.$file)){
					@unlink('.'.$file);
				}
			}
            $this->renderSuccess('图片删除成功');
        }
		$this->renderError('图片删除失败');
	}

    
	private function addUploadFile($group_id, $fileDir, $fileName, $fileInfo, $fileType)
    {
		// $this->config = $this->setting['storage'];
        // 存储引擎
        $storage = $this->config['default'];
        // 存储域名
        $fileUrl = isset($this->config['engine'][$storage]['domain'])
            ? $this->config['engine'][$storage]['domain'].$fileDir.'/' : $fileDir.'/';
		// 添加文件库记录
		$data = [
            'group_id' => $group_id > 0 ? (int)$group_id : 0,
            'storage' => $storage,
            'file_url' => $fileUrl,
            'file_name' => $fileName,
            'file_size' => $fileInfo['size'],
            'file_type' => $fileType,
			'extension' => pathinfo($fileInfo['name'], PATHINFO_EXTENSION),
			'company_id' => $this->company['id'],
			// 'admin_id' => $this->admin['id'],
        ];
        $data['file_id'] = $this->upload_fileObj->add($data);
        return $data;
	}
	
}